using System;
using System.Linq;
using System.Reflection;
using System.Text;
using PropertyExpression.Common;

namespace PropertyExpression.PresentationCode.CodeGenerator
{
    internal static class InternalUtil
    {
        internal static void BuildPrefixMethod(Type propertyProviderType, StringBuilder stringBuilder, Type parameterType, IOverloadedAndPrefixMethodsBuilder overloadedAndPrefixMethodsBuilder)
        {
            foreach (var staticPropertyInfo
                in propertyProviderType.GetProperties(BindingFlags.Public | BindingFlags.Static))
            {
                if (parameterType.IsAssignableFrom(staticPropertyInfo.PropertyType))
                {
                    overloadedAndPrefixMethodsBuilder.BuildMethodsWithPrefix(stringBuilder, staticPropertyInfo.Name, propertyProviderType);
                }
            }
        }

        internal static string GetCSharTypeName(Type type)
        {
            if (!type.IsGenericType)
            {
                return type.ToString();
            }

            var typeName = type.Name;
            return string.Format(
                "{0}.{1}<{2}>",
                type.Namespace,
                typeName.Substring(0, typeName.IndexOf('`')),
                type.GetGenericArguments()
                    .Select(argType => GetCSharTypeName(argType))
                    .Join(", "));
        }
    }
}